// pages/development/development.js
let app = getApp()
let api = require("../../utils/api.js")

Page({

    /**
     * 页面的初始数据
     */
    data: {
        oType: 1,
        id: 0,
        consult: {},
        accountid: 0,
        typeid: 0,
        day: '',
        timer: '',
        weekday: '',
        startvalue: '',
        isfollow: 0,
        orderinfo: [{
            day: '',
            timer: '',
            startvalue: '',
            conslultid: '',
            typeid: 0,
            weekday: ''
        }]
    },
	preview(e){
		console.log(e)
		wx.previewImage({
			urls: e.currentTarget.dataset.photos,
		})
	},
    customer: function(e) {
		wx.navigateTo({
			url: '/pages/thetext/thetext?id=' + e.currentTarget.dataset.id,
		})
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this

        that.setData({
            id: options.id
        })

    },
    person: function(e) {
        var that = this;
        that.setData({
            oType: 1,
        })
    },
    tableCheck1: function(e) {
        var that = this;
        that.setData({
            oType: 2,
        })
    },
    selecttimer: function(res) {
        this.setData({
            day: res.currentTarget.dataset.day,
            timer: res.currentTarget.dataset.value,
            startvalue: res.currentTarget.dataset.startvalue,
            weekday: res.currentTarget.dataset.weekday,
        })
    },
    follow: function(e) {
        let that = this
        wx.getStorage({
            key: 'userInfo',
            success: function(res) {
                // success
                let data = { account_id: that.data.accountid, id: that.data.id, type: 'consult' }
                let url = app.u.HOME_URL + app.u.FOLLOW
                api.requestUrl(data, url).then(res => {
                    that.setData({
                        isfollow: res.data
                    })
                })
            },
            fail: function() {
                // fail
                api.showError('请先登录')
                setTimeout(function() {
                    wx.navigateTo({
                        url: '../login/login'
                    })
                }, 1000)
            },
            complete: function() {
                // complete
            }
        })

    },
    rorder: function(e) {
        let that = this
        let res = Array();
        if (that.data.accountid == 0) {
            api.showError("请先登录账号")
            setTimeout(function() {
                wx.navigateTo({
                    url: '../login/login'
                })
            }, 1000)
            return false
        }
        var up = "orderinfo.day";
        var up1 = "orderinfo.timer";
        var up2 = "orderinfo.startvalue";
        var up3 = "orderinfo.conslultid";
        var up4 = "orderinfo.typeid";
        var up5 = "orderinfo.weekday";
        this.setData({
            [up]: that.data.day,
            [up1]: that.data.timer,
            [up2]: that.data.startvalue,
            [up3]: that.data.id,
            [up4]: that.data.typeid,
            [up5]: that.data.weekday,
        })
        wx.setStorage({
            key: 'rorderinfo',
            data: that.data.orderinfo,
            success: function() {
                wx.navigateTo({
                    url: '../developorder/developorder?id=' + that.data.id
                })
            }
        })
    },
    consulting: function(e) {
        this.setData({
            typeid: e.currentTarget.dataset.id,
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        wx.showLoading()
        let that = this
        let url = app.u.HOME_URL + app.u.CONSULT_VIEW
        let accountid = 0
        wx.getStorage({
            key: 'userInfo',
            success: function(res) {
                accountid = 0;
                if (res.data.id) {
                    accountid = res.data.id
                }

                // success
                that.setData({
                    accountid: accountid
                })
                let data = { id: that.data.id, account_id: accountid }
                api.requestUrl(data, url).then(res => {
                    wx.hideLoading()
                    if (res.code == 200) {
						if (res.data.basic.type[0]){
							that.setData({
								typeid: res.data.basic.type[0].id,
							})
						}
                        that.setData({
                            consult: res.data,
                            isfollow: res.data.basic.isfollow
                        })
                    } else {
                        api.showError(res.msg)
                    }
                })
            },
            fail: function() {
                let data = { id: that.data.id, accountid: accountid }
                api.requestUrl(data, url).then(res => {
                    wx.hideLoading()
                    if (res.code == 200) {
						if (res.data.basic.type[0]) {
							that.setData({
								typeid: res.data.basic.type[0].id,
							})
						}
						that.setData({
							consult: res.data,
							isfollow: res.data.basic.isfollow
						})
                    } else {
                        api.showError(res.msg)
                    }
                })
            }
        })
        wx.showShareMenu({
            withShareTicket: true
        })

    }
})